<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/20
 * Time: 2:34
 */
class Cate
{
//组合一维数组
    static public function getTree($data, $pid = 0, $level = '0', $html = '&nbsp;&nbsp;└')
    {
        $arr = array();
        foreach ($data as $v) {
            if ($v->cate_pid == $pid) {
                $v->level = $level + 1;
                $v->html = str_repeat($html, $level);
                $arr[] = $v;
                $arr = array_merge($arr, self::getTree($data, $v->cate_id, $level + 1));
            }
        }
        return $arr;
    }

//根据传递过来的id递归查找其所有子集id
    static public function getChildIds($data, $pid)
    {
        $arr = array();
        foreach ($data as $v) {
            if ($v->cate_pid == $pid) {
                $arr[] = $v->cate_id;
                $arr = array_merge($arr, self::getChildIds($data, $v->cate_id));
            }
        }
        return $arr;
    }

}